﻿using Rapid.Core.Aop;
using Rapid.Entity.Basic;
using Rapid.Utility.Helper;
using Rapid.WebNecessary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Rapid.MvcExtension.Controls.Manage
{
    public static class Easyui
    {
        #region 按钮模块

        /// <summary>
        /// 自定义按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="func">方法</param>
        /// <returns></returns>
        public static MvcHtmlString LinkButton(string icon, string text, string func, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-linkbutton\"");

            sb.Append(" plain=\"" + plain.ToString().ToLower() + "\"");

            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append(" iconCls=\"" + icon + "\"");
            }
            if (!string.IsNullOrEmpty(func))
            {
                sb.Append(" onclick=\"" + func + "\"");
            }

            sb.Append(">" + text + "</a>");

            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义图标按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="func">方法</param>
        /// <returns></returns>
        public static MvcHtmlString IconButton(string icon, string text, string func, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-linkbutton\"");

            sb.Append(" iconCls=\"" + icon + "\"");
            sb.Append(" plain=\"" + plain.ToString().ToLower() + "\"");

            if (!string.IsNullOrEmpty(func))
            {
                sb.Append(" onclick=\"" + func + "\"");
            }

            sb.Append(" title=\"" + text + "\"></a>");

            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义菜单按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="menu">菜单ID</param>
        /// <returns></returns>
        public static MvcHtmlString MenuButton(string icon, string text, string menu, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-menubutton\"");

            sb.Append(" plain=\"" + plain.ToString().ToLower() + "\"");

            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append(" iconCls=\"" + icon + "\"");
            }
            if (!string.IsNullOrEmpty(menu))
            {
                sb.Append(" menu=\"" + menu + "\"");
            }

            sb.Append(">" + text + "</a>");

            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义图标菜单按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="menu">菜单ID</param>
        /// <returns></returns>
        public static MvcHtmlString IconMenuButton(string icon, string text, string menu, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-menubutton\"");

            sb.Append(" plain=\"" + plain.ToString().ToLower() + "\"");

            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append(" iconCls=\"" + icon + "\"");
            }
            if (!string.IsNullOrEmpty(menu))
            {
                sb.Append(" menu=\"" + menu + "\"");
            }

            sb.Append(" title=\"" + text + "\"></a>");

            return new MvcHtmlString(sb.ToString());
        }

        #endregion

        #region 菜单模块

        /// <summary>
        /// 后台主菜单
        /// </summary>
        /// <returns></returns>
        public static MvcHtmlString MainMenu()
        {
            var menus = ServiceFactory.BasicMenu.GetByUserId(SessionFactory.UserID, SessionFactory.IsSuper);

            StringBuilder sb = new StringBuilder();

            foreach (var model in menus)
            {
                if (model.Pid.Equals("0") && model.Type.Equals("null"))
                {
                    sb.Append("<div title=\"" + model.Name + "\" ");
                    sb.Append("data-options=\"iconCls:'" + model.Icon + "'\">");
                    sb.Append("<ul class=\"easyui-tree\" ");
                    sb.Append("data-options=\"lines:true,");
                    sb.Append("loadFilter:manage.menuLoadFilter,");
                    sb.Append("onClick:manage.menuOnClick\">");
                    sb.Append(MainChildrenMenus(model.ID, menus, true));
                    sb.Append("</ul>");
                    sb.Append("</div>");
                }
            }

            return new MvcHtmlString(sb.ToString());
        }

        private static string MainChildrenMenus(string pid, List<BasicMenu> menus, bool first)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in menus)
            {
                if (item.Pid.Equals(pid) && !item.Type.Equals("embed"))
                {
                    string href = item.Link + "?_menuid=" + item.ID;

                    sb.Append("<li iconCls=\"" + item.Icon + "\" ");
                    sb.Append("data-options=\"attributes:");
                    sb.Append("{href:'" + href + "',type:'" + item.Type + "'}\">");
                    sb.Append("<span>" + item.Name + "</span>");
                    sb.Append(MainChildrenMenus(item.ID, menus, false));
                    sb.Append("</li>");
                }
            }

            if (!string.IsNullOrEmpty(sb.ToString()) && !first)
            {
                return "<ul>" + sb.ToString() + "</ul>";
            }

            return sb.ToString();
        }

        #endregion

        #region 页面模块

        /// <summary>
        /// Radio项
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <param name="value">样本</param>
        /// <param name="text">文本</param>
        /// <param name="content">值</param>
        /// <returns></returns>
        public static MvcHtmlString RadioItem(string name, string value, string text, string content)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<input name=\"" + name + "\" type=\"radio\" ");
            sb.Append("value=\"" + value + "\" label=\"" + text + "\" ");
            if (value.Equals(content))
            {
                sb.Append("checked=\"checked\" ");
            }
            sb.Append("/>");

            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 内嵌页面
        /// </summary>
        /// <param name="tag">页面标记</param>
        /// <returns></returns>
        public static MvcHtmlString Embed(string tag)
        {
            string menuid = HttpHelper.QueryString("_menuid");

            BasicMenu embed = ServiceFactory.BasicMenu.GetEmbed(menuid, tag);

            string link = string.Empty;

            if (embed != null)
            {
                link = embed.Link + "?_menuid=" + embed.ID;
            }

            return new MvcHtmlString(link);
        }

        /// <summary>
        /// 页面功能
        /// </summary>
        /// <returns></returns>
        public static MvcHtmlString Toolbar(BasicMenu menu = null)
        {
            if (menu == null)
            {
                string menuid = HttpHelper.QueryString("_menuid");
                menu = ServiceFactory.BasicMenu.Get(menuid);
            }

            var list = ServiceFactory.BasicFunction.GetByUserId(menu.ID, SessionFactory.UserID, SessionFactory.IsSuper);

            StringBuilder sb = new StringBuilder();

            foreach (var item in list)
            {
                sb.Append(Easyui.LinkButton(item.Icon, item.Name, menu.Module + menu.Tag + "." + item.Func + "()", true));
            }

            return new MvcHtmlString(sb.ToString());
        }

        #endregion
    }
}